package com.longdaji.decoration.ui.activitiesOfMine.pendingDelivery;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.PendingDeliveryOrderAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PendingDeliveryActivity extends BaseActivity implements PendingDeliveryContract.View {

    @Inject
    PendingDeliveryContract.Presenter mPresenter;

    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.rv_pending_delivery)
    RecyclerView rvPendingDelivery;
    @BindView(R.id.sr_pending_delivery)
    SwipeRefreshLayout srPendingDelivery;

    private PendingDeliveryOrderAdapter mAdapter;
    private List<Orders.OrdersBean.OrderBean> mOrderList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pending_delivery);
        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("待发货");

        initView();


    }

    private void initView() {
        rvPendingDelivery.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PendingDeliveryOrderAdapter(mOrderList);
        mAdapter.setCancelOrderListener(new PendingDeliveryOrderAdapter.CancelOrderListener() {
            @Override
            public void onCancelOrderClick(int position) {
                mPresenter.onCancelOrderClick(Account.getInstance().getUserid(), mOrderList.get(position).getOrderNo());
            }
        });
        rvPendingDelivery.setAdapter(mAdapter);
        srPendingDelivery.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        srPendingDelivery.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
        mPresenter.initData();

    }

    private void refreshData() {
        mPresenter.initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showPendingDeliveryOrders(List<Orders.OrdersBean.OrderBean> pendingDeliveryOrderList) {
        Collections.reverse(pendingDeliveryOrderList);
        if (pendingDeliveryOrderList.size() > 0) {
            mAdapter.setData(pendingDeliveryOrderList);
        } else {
            Toast.makeText(this, "无待发货订单", Toast.LENGTH_SHORT).show();
            mAdapter.clearData();
        }
        if (srPendingDelivery != null && srPendingDelivery.isRefreshing()) {
            srPendingDelivery.setRefreshing(false);
        }
    }

    @Override
    public void showCancelOrderSucceed() {
        Toast.makeText(this, "取消订单成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showRefundSucceed() {
        Toast.makeText(this, "退款申请成功", Toast.LENGTH_SHORT).show();
    }
}
